import requests
from cfg import morning_msg
from lxml import etree

# 企业ID
corpid = 'ww7764aa04654ca388'


def get_weather():
    url = 'https://tianqi.2345.com/zhushan1d/60528.htm'
    response = requests.get(url)
    text = response.text
    etr = etree.HTML(text)

    # 今日天气
    today_wea = etr.xpath('//*[@id="today-main-deatil"]/div[2]/div[2]/div[1]/div[2]/span//text()')[0]
    # 天气
    wea = etr.xpath('//*[@id="today-main-deatil"]/div[2]/div[2]/div[1]/div[1]/em//text()')[0]
    # 温度
    temp = etr.xpath('//*[@id="today-main-deatil"]/div[2]/div[2]/div[1]/div[1]/span//text()')[0]
    # 风量
    air_volume = etr.xpath('//*[@id="today-main-deatil"]/div[2]/div[2]/div[1]/ul/li/span[1]//text()')[0]
    # 湿度
    humidity = etr.xpath('//*[@id="today-main-deatil"]/div[2]/div[2]/div[1]/ul/li/span[2]//text()')[0]
    # 紫外线
    ultrav_radiation = etr.xpath('//*[@id="today-main-deatil"]/div[2]/div[2]/div[1]/ul/li/span[3]//text()')[0]
    result = {
        'today_weather': today_wea,
        'weather': wea,
        'temp': temp,
        'air_volume': air_volume,
        'humidity': humidity,
        'ultrav_radiation': ultrav_radiation,
    }
    return result


def send_msg(msg):
    url = 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=e249fa17-4d1b-4d02-9121-fdbcbe33b71e'
    data = {
        "msgtype": "text",
        "text": {
            "content": msg,
            "mentioned_list": ["@all"],
        }
    }
    response = requests.post(url, json=data)
    print(response.text)


if __name__ == '__main__':
    data = get_weather()
    msg = f'湖北十堰竹山县 \n今日天气：{data["today_weather"]}\n当前天气：{data["weather"]}\n当前温度：{data["temp"]}\n风量：{data["air_volume"]}\n湿度：{data["humidity"]}\n紫外线：{data["ultrav_radiation"]}'
    send_msg(msg)
